﻿using HBKY.Shop.Common.Extension;
using HBKY.Shop.Extensions.AuthHelper.Policys;
using HBKY.Shop.Model.Enum;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace ZQCY.Watchmen.Extensions.AuthHelper.Policys
{
    /// <summary>
    /// 授权返回值
    /// </summary>
    public class ApiResponseHandler : AuthenticationHandler<AuthenticationSchemeOptions>
    {
        public ApiResponseHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger,
            UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
        {
        }

        protected override Task<AuthenticateResult> HandleAuthenticateAsync()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///  处理jwt 授权失败验证
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
        {
            Response.ContentType = "application/json";
            Response.StatusCode = StatusCodeEnum.Status401Unauthorized.Key();
            await Response.WriteAsync(
                JsonConvert.SerializeObject((new ApiResponse(StatusCodeEnum.Status401Unauthorized)).responseMessage));
        }

        /// <summary>
        /// 处理jwt 授权失败验证
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
        {
            Response.ContentType = "application/json";
            Response.StatusCode = StatusCodeEnum.Status403Unauthorized.Key();
            await Response.WriteAsync(
                JsonConvert.SerializeObject((new ApiResponse(StatusCodeEnum.Status403Unauthorized)).responseMessage));
        }
    }
}